package threelayer.controller;

import threelayer.bean.User;
import threelayer.service.UserService;
import threelayer.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Title：
 * Description：
 * Created by Myth on 5/15/2017.
 */
@WebServlet(name = "LoginController",urlPatterns = "/LoginController")
public class LoginController extends HttpServlet {
    //调用业务层
    private UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User loginUser = userService.login(username,password);
        //根据业务层处理结果控制页面跳转
        if (loginUser != null) {
            //如果登录成功，将获得的bean放到Session中
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("loginUser", loginUser);
            //页面转发
            request.getRequestDispatcher("/WEB-INF/threelayer/success.jsp").forward(request,response);
        } else {
            request.getRequestDispatcher("/WEB-INF/threelayer/error.jsp").forward(request,response);
        }
    }
}
